{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "结果： 30\n"
     ]
    }
   ],
   "source": [
    "def func_out(num1):\n",
    "    def func_in(num2):\n",
    "        return num1+num2\n",
    "    return func_in\n",
    "f=func_out(10)\n",
    "result=f(20)\n",
    "print('结果：',result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def log(func):\n",
    "    def wrapper(a, b):\n",
    "        func(\"%s和%s相约鹊桥\"%(a,b))\n",
    "        func(\"%s和%s看电影\"%(a,b))\n",
    "        func(\"%s和%s明年见\"%(a,b))\n",
    "    return wrapper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "牛郎和织女相约鹊桥\n",
      "牛郎和织女看点电影\n",
      "牛郎和织女明年见\n"
     ]
    }
   ],
   "source": [
    "log(print)(\"牛郎\", \"织女\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "装饰器 decorator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def log(func):\n",
    "    def wrapper():\n",
    "        print(\"约会开始了\")\n",
    "        func()\n",
    "        print(\"明年见\")\n",
    "    return wrapper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "@log\n",
    "def my_deca():\n",
    "    print(\"牛郎和织女看电影\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "约会开始了\n",
      "牛郎和织女看电影\n",
      "明年见\n"
     ]
    }
   ],
   "source": [
    "my_deca()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
